<template>
  <div class="app-container">
    <div class="header">
      <div class="word">
        <div class="w1">
          打包管理系统
        </div>
        <div class="w2">
          在打包管理系统中你可以查看打包状态等基本信息，同时还可以实现查询原料打包状态，添加原料打包信息等功能，提供一套可行的打包管理方案
        </div>
        <div class="w2">
          原料打包系统是加工系统中重要的一环,它可以为我们提供原料打包的信息，为下一步加工做准备
        </div>
      </div>
      <img class="imgDri" src="../static/u=2402425779,1581852191&fm=253&fmt=auto&app=138&f=JPEG.webp">
    </div>
    <div class="index">
      <div class="searchHead">
        <el-button type="primary" @click="outerVisible = true">新增包装表</el-button>
        <el-input v-model="input3" placeholder="请输入要查询的数据" class="searchIuput">
          <el-select slot="prepend" v-model="select" class="sel3" placeholder="请选择" style="width: 120px;">
            <el-option label="产品名字" value="productName" />
            <el-option label="员工名字" value="staffName" />
          </el-select>
          <el-button slot="append" v-model="searchInf" icon="el-icon-search" @click="searchMes" />
        </el-input>
      </div>
      <el-dialog title="新增包装表" :visible.sync="outerVisible">
        <el-dialog width="75%" title="所有质检表" :visible.sync="innerVisible" append-to-body>
          <template>
            <el-table :data="deliver" stripe style="width: 100%">
              <el-table-column prop="inspectionId" label="原料质检id" width="180" />
              <el-table-column prop="productName" label="产品名字" width="100" />
              <el-table-column prop="weightGoods" label="产品吨数" width="100" />
              <el-table-column prop="staffName" label="质检员" width="90" />
              <el-table-column prop="staffPhone" label="联系电话" width="120" />
              <el-table-column prop="staffPosition" label="员工职位" width="90" />
              <el-table-column prop="tinyCreateContent" label="微生物指标" width="120" />
              <el-table-column prop="drugResidue" label="药品残留量" width="120" />
              <el-table-column prop="weigihtOk" label="质量是否合格" width="120" />
            </el-table>
          </template>
          <div class="page">
            <el-pagination
              :page-size="5"
              layout="prev, pager, next, jumper"
              :total="deliverPageSize"
              @size-change="handleSizeChange"
              @current-change="handleDeliverChange"
            />
          </div>
        </el-dialog>
        <el-form class="form">
          <el-form-item class="item" label="产品质检id" :label-width="formLabelWidth">
            <el-input v-model="addMes.producitInspectionId" class="in" autocomplete="off" />
          </el-form-item>
          <el-form-item class="item" label="产品名称" :label-width="formLabelWidth">
            <el-input v-model="addMes.productName" class="in" autocomplete="off" />
          </el-form-item>
          <el-form-item class="item" label="产品总吨数" :label-width="formLabelWidth">
            <el-input v-model="addMes.productWeight" class="in" autocomplete="off" />
          </el-form-item>
          <el-form-item class="item" label="打包人员" :label-width="formLabelWidth">
            <el-input v-model="addMes.staffName" class="in" autocomplete="off" />
          </el-form-item>
          <!-- <el-form-item class="item" label="联系电话" :label-width="formLabelWidth">
            <el-input v-model="addMes.staffPhone" class="in" autocomplete="off" />
          </el-form-item> -->
          <!-- <el-form-item class="item" label="员工职位" :label-width="formLabelWidth">
            <el-input v-model="addMes.staffPosition" class="in" autocomplete="off" />
          </el-form-item> -->
          <el-form-item class="item" label="打包数量" :label-width="formLabelWidth">
            <el-input v-model="addMes.packageNumber" class="in" autocomplete="off" />
          </el-form-item>
          <el-form-item class="item" label="打包规格" :label-width="formLabelWidth">
            <el-input v-model="addMes.packageSpecification" class="in" autocomplete="off" />
          </el-form-item>
          <div style="margin-left:52px">
            <label style="height: 36px;line-height: 36px;padding: 0 12px 0 0;">打包方式</label>
            <el-select v-model="addMes.tinyCreateContent" class="sel2" placeholder="请选择" style="width: 186px;">
              <el-option label="盒装" value="盒装" />
              <el-option label="袋装" value="袋装" />
            </el-select>
          </div>
          <el-form-item class="item" label="备注" :label-width="formLabelWidth">
            <el-input v-model="addMes.remarks" class="in" autocomplete="off" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" plain @click="showDeliver()">查看所有产品质检表</el-button>
          <el-button @click="outerVisible = false">取 消</el-button>
          <el-button type="primary" @click="addDri()">确 定</el-button>

        </div>
      </el-dialog>

      <el-table :data="mes" type="" style="width: 100%">
        <el-table-column prop="producitInspectionId" label="产品质检id" width="180" />
        <el-table-column prop="productName" label="产品名称" width="110" />
        <el-table-column prop="productWeight" label="产品总吨数" width="100" />
        <el-table-column prop="staffName" label="打包人员" width="90" />
        <el-table-column prop="staffPhone" label="联系电话" width="120" />
        <el-table-column prop="staffPosition" label="员工职位" width="90" />
        <el-table-column prop="packageNumber" label="打包数量" width="110" />
        <el-table-column prop="packageSpecification" label="打包规格" width="110" />
        <el-table-column prop="packageType" label="打包方式（袋装盒装等）" />
        <el-table-column prop="remarks" label="备注" />
        <el-table-column fixed="right" label="操作" width="100">
          <template slot-scope="scope">
            <el-button type="text" @click="changeMsg(mes[scope.$index],scope.$index)">修改</el-button>
            <el-button type="text" style="color:red" @click="mes[scope.$index].isShowDel = true">删除</el-button>

            <el-dialog :modal-append-to-body="false" title="提示" :visible.sync="mes[scope.$index].isShowDel" width="30%">
              <span>确认要删除此条信息吗？</span>
              <span slot="footer" class="dialog-footer">
                <el-button @click="mes[scope.$index].isShowDel = false">取 消</el-button>
                <el-button type="primary" @click="driveDel(scope.$index)">确 定</el-button>
              </span>
            </el-dialog>

            <el-dialog
              :modal-append-to-body="false"
              title="修改信息"
              :visible.sync="mes[scope.$index].isShow"
              :before-close="handleClose"
            >
              <el-form class="form">
                <el-form-item class="item" label="产品名称" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].productName" autocomplete="off" disabled />
                </el-form-item>
                <el-form-item class="item" label="产品总吨数" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].productWeight" autocomplete="off" disabled />
                </el-form-item>
                <el-form-item class="item" label="打包人员" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].staffName" autocomplete="off" />
                </el-form-item>
                <el-form-item class="item" label="员工电话" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].staffPhone" autocomplete="off" disabled />
                </el-form-item>
                <el-form-item class="item" label="员工职位" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].staffPosition" autocomplete="off" disabled />
                </el-form-item>
                <el-form-item class="item" label="打包数量" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].packageNumber" autocomplete="off" />
                </el-form-item>
                <el-form-item class="item" label="打包规格" :label-width="formLabelWidth">
                  <el-input v-model="mes[scope.$index].packageSpecification" autocomplete="off" disabled />
                </el-form-item>
                <div class="sel">
                  <label style="height: 36px;line-height: 36px;padding: 0 12px 0 0;">打包方式</label>
                  <el-select v-model="mes[scope.$index].packageType" class="sel2" placeholder="请选择">
                    <el-option label="盒装" value="盒装" />
                    <el-option label="袋装" value="袋装" />
                  </el-select>
                </div>
                <el-form-item class="item" label="备注" :label-width="formLabelWidth">
                  <el-input
                    v-model="mes[scope.$index].remarks"
                    autocomplete="off"
                    type="textarea"
                    style="width: 350px;"
                  />
                </el-form-item>
              </el-form>

              <div slot="footer" class="dialog-footer">
                <el-button @click="cancelChangeMsg(scope.$index)">取 消</el-button>
                <el-button type="primary" @click="changeInspectionForm(scope.$index)">确 定</el-button>
              </div>

            </el-dialog>
          </template>
        </el-table-column>
      </el-table>
      <div class="page">
        <el-pagination
          :page-size="5"
          layout="prev, pager, next, jumper"
          :total="pageSize"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script>
import request from '../../../utils/request'
export default {
  name: 'MaterialTesting',
  data() {
    return {
      mes: [],
      firstMes: [],
      allMes: [],
      deliver: [],
      index: null,
      dialogTableVisible: false,
      formLabelWidth: '120px',
      addMes: {
        // "deliverId": null,
        // "drugResidue": '',
        // // "gmtCreate": '',
        // // "gmtModified": '',
        // // "inspecitonJudge": null,
        // "inspectionId": null,
        // // "isDelete": 0,
        // "mateiralName": "",
        // "positionId": null,
        // "staffId": null,
        // "staffName": "",
        // "staffPhone": "",
        // "staffPosition": "",
        // "tinyCreateContent": "",
        // "weightGoods": null,
        // "weigihtOk": ""
      },
      dialogFormVisible: false,
      outerVisible: false,
      innerVisible: false,
      pageSize: null,
      pageNum: 1,
      deliverPageNum: 1,
      deliverPageSize: null,
      select: '',
      searchInf: '',
      input3: '',
      total: null
    }
  },
  async mounted() {
    this.getDrive()
  },
  methods: {
    async getDrive() {
      await request({
        method: 'get',
        url: '/prot/port-package/getAll/' + this.pageNum + '/5'
      }).then((response) => {
        console.log(response)
        this.mes = response.data.records
        for (let i = 0; i < this.mes.length; i++) {
          this.$set(this.mes[i], 'isShow', false)
          this.$set(this.mes[i], 'isShowDel', false)
        }
        this.pageSize = response.data.total
        this.total = response.data.total
      })
    },
    changeMsg(mesIndex, index) {
      mesIndex.isShow = true
      this.firstMes = JSON.parse(JSON.stringify(this.mes))[index]
      this.index = index
      console.log('this.firstMes', this.firstMes)
    },
    async delMsg(index) {
      // this.mes[index].isShowDel = true
      console.log('index', index, this.mes)
      console.log(this.mes[index].materialHouseId)
      await request({
        method: 'delete',
        url: '/prot/port-package/deleteById/' + this.mes[index].packageId
      }).then(response => {
        // console.log('response', response)
        if (response.statusCode == 20000) {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '删除失败',
            type: 'warning'
          })
        }
        this.getDrive()
      })
    },
    async changeInspectionForm(index) {
      var changMes = JSON.parse(JSON.stringify(this.mes))[index]
      console.log(changMes, '1111')
      console.log('changMes', changMes)
      await request({
        method: 'get',
        url: '/prot/staff/getByName/' + changMes.staffName
      }).then(response => {
        changMes.staffId = response.data[0].staffId
        console.log(response, '99999')
      })
      var newChangMes = {
        packageId: changMes.packageId,
        staffId: changMes.staffId,
        packageType: changMes.packageType,
        // packageSpecification: changMes.packageSpecification,
        packageNumber: changMes.packageNumber,
        remarks: changMes.remarks
        // productWeight: changMes.productWeight
      }
      console.log(newChangMes)
      await request({
        method: 'post',
        url: '/prot/port-package/updatePortpack/' + changMes.packageId,
        data: newChangMes
      }).then(response => {
        console.log(response, '@@', changMes)
        if (response.statusCode == 20000) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '发生错误，修改失败',
            type: 'warning'
          })
        }
        this.mes[index].isShow = false
      })
    },
    cancelChangeMsg(index) {
      this.mes[index].isShow = false
      // console.log(111);
      this.mes[index] = JSON.parse(JSON.stringify(this.firstMes))
      console.log(this.mes[index])
      this.getDrive()
    },
    async driveDel(index) {
      await request({
        method: 'delete',
        url: '/prot/port-package/deleteById/' + this.mes[index].packageId
      }).then(response => {
        console.log(response)
        if (response.statusCode == 20000) {
          this.dialogFormVisible = false
          this.$message({
            message: '删除成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '发生错误，删除失败',
            type: 'warning'
          })
        }
      })
      this.getDrive()
      this.mes[index].isShowDel = false
    },

    async showDeliver() {
      this.innerVisible = true
      await request({
        method: 'get',
        url: '/prot/product-inspection/getAll/' + 1 + '/' + 5
      }).then(response => {
        console.log('showDeliver', response)
        this.deliver = JSON.parse(JSON.stringify(response.data.records))
        console.log(this.deliver)
        this.deliverPageSize = response.data.total
      })
    },
    async addDri() {
      await request({
        method: 'get',
        url: '/prot/staff/getByName/' + this.addMes.staffName
      }).then(response => {
        // console.log('员工', response);
        this.addMes.staffId = response.data[0].staffId
      })
      console.log('this.addMes', this.addMes)
      request({
        method: 'post',
        url: '/prot/port-package/addPortPackage',
        data: this.addMes
      }).then(response => {
        // console.log(response)
        if (response.statusCode == 20000) {
          this.addMes = {}
          this.getDrive()
          this.$message({
            message: '添加成功',
            type: 'success'
          })
        } else {
          this.$message({
            message: '发生错误，添加失败',
            type: 'warning'
          })
        }
      })
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.pageNum = val
      request({
        method: 'get',
        url: '/prot/port-package/getAll/' + val + '/5'
      }).then((response) => {
        // console.log(response)
        this.mes = response.data.records
        for (let i = 0; i < this.mes.length; i++) {
          this.$set(this.mes[i], 'isShow', false)
          this.$set(this.mes[i], 'isShowDel', false)
        }
      })
    },
    handleDeliverChange(val) {
      this.deliverPageNum = val
      request({
        method: 'get',
        url: '/prot/product-inspection/getAll/' + val + '/5'
      }).then((response) => {
        // console.log(response)
        this.deliver = JSON.parse(JSON.stringify(response.data.records))
      })
    },

    searchMes() {
      // console.log('this.mes', this.mes);
      if (this.select == 'productName') {
        const data = {
          productName: this.input3
        }
        request({
          url: '/prot/port-package/getAllByProductName/' + data.productName + '/1/5',
          method: 'get'
        }).then(response => {
          if (response.statusCode == 20000) {
            this.$message({
              message: '查询成功',
              type: 'success'
            })
            // console.log(response)
            this.mes = response.data.records
            for (let i = 0; i < this.mes.length; i++) {
              this.$set(this.mes[i], 'isShow', false)
              this.$set(this.mes[i], 'isShowDel', false)
            }
            this.pageSize = response.data.total
          } else {
            this.$message({
              message: '查无该产品，查询失败',
              type: 'warning'
            })
          }
        })
      } else if (this.select == 'staffName') {
        const data = {
          staffName: this.input3
        }
        request({
          url: '/prot/port-package/getAllByStaffName/' + data.staffName + '/1/5',
          method: 'get'
        }).then(response => {
          // console.log('searchMes2', response);
          if (response.statusCode == 20000) {
            this.$message({
              message: '查询成功',
              type: 'success'
            })
            this.mes = response.data.records
            console.log('response', response)
            for (let i = 0; i < this.mes.length; i++) {
              this.$set(this.mes[i], 'isShow', false)
              this.$set(this.mes[i], 'isShowDel', false)
            }
            this.pageSize = response.data.total
          } else {
            this.$message({
              message: '查无该员工，查询失败',
              type: 'warning'
            })
          }
        })
      }
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
          this.addMes = {}
          this.cancelChangeMsg(this.index)
        })
        .catch(_ => { })
    }
  }
}
</script>

<style lang="scss" scoped>
.header {
  width: 100%;
  height: 350px;
  // border: 1px solid black;
  display: flex;
  margin-bottom: 20px;
  justify-content: space-between;
}

.form {
  display: flex;
  flex-wrap: wrap;
  // border: 1px solid black;
  // justify-content: space-around;
}

.sel {
  width: 360px;
  display: flex;
  height: 50px;
  // border: 1px solid black;
  // border: 1px solid black;
  justify-content: flex-end;
  align-content: center;
  // border: 1px solid black;
}

.sel2 {
  width: 240px;
}

.page {
  width: 100%;
  display: flex;
  justify-content: right;
}

.imgDri {
  height: 300px;
  width: 400px;
}

.w1 {
  height: 80px;
  font-size: 25px;
  font-weight: 350;
}

.w2 {
  height: 50px;
  font-size: 15px;
  font-weight: 250;
}

.searchHead {
  display: flex;
  justify-content: space-between;
}

.searchIuput {
  width: 350px;
}

.sel3 {
  width: 120px;
}
</style>

